//
//  PlayChatCell.m
//  Runner
//
//  Created by 小二同學 on 2020/3/31.
//  Copyright © 2020 The Chromium Authors. All rights reserved.
//

#import "PlayChatCell.h"

@interface PlayChatCell ()

/** 姓名 */
@property (nonatomic, strong) UILabel *name;

/** 文字 */
@property (nonatomic, strong) UILabel *content;
@end

@implementation PlayChatCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.name = [[UILabel alloc] init];
        self.name.font = [UIFont systemFontOfSize:13];
        self.name.textColor = UIColorFromRGB(0xff919699);
        self.name.numberOfLines = 1;
        [self.contentView addSubview:self.name];
        
        [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(15);
        }];
        
        self.content = [[UILabel alloc] init];
        self.content.font = [UIFont systemFontOfSize:15];
        self.content.numberOfLines = 0;
        self.content.textColor = UIColorFromRGB(0xff212426);
        [self.contentView addSubview:self.content];
        
        [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.name.mas_bottom).offset(5);
            make.right.equalTo(self.contentView).offset(-15);
            make.left.equalTo(self.name);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
        
        [self.content setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
                                                forAxis:UILayoutConstraintAxisHorizontal];
        [self.name setContentCompressionResistancePriority:UILayoutPriorityRequired
                                                forAxis:UILayoutConstraintAxisHorizontal];

    }
    return self;
}

- (void)sendChatInfo:(NSDictionary *)dic {
    self.name.text = [NSString stringWithFormat:@"%@:",dic[@"nickname"]];
    self.content.attributedText = [self getHtmlStringWithString:[NSString stringWithFormat:@"%@",dic[@"content"]]];
}

- (NSMutableAttributedString *)getHtmlStringWithString:(NSString *)string {
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)};
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    
    // 设置段落格式
    NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
    para.lineSpacing = 8;
//    para.paragraphSpacing = 10;
    [attStr addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(0, attStr.length)];
    
    // 设置文本的Font
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attStr.length)];
    
    return attStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
